package me.xiaoyan.ehr.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.UUID;

@Service
public class MailUtil {
    private JavaMailSender javaMailSender;

    @Autowired
    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    public void sendSimple(String email, String text, String subject) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(subject);
        message.setText(text);
        message.setTo(email);
        message.setFrom("yaclty@yandex.com");
        javaMailSender.send(message);
    }

    public void sendHTML(String email, String html, String subject) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setText(html, true);
        helper.setSubject(subject);
        helper.setTo(email);
        helper.setFrom("yaclty@yandex.com");
        message.setFrom("yaclty@yandex.com");
        javaMailSender.send(message);
    }
}
